﻿using Microsoft.Extensions.Configuration;
using System.Collections.Generic;

namespace MingYuanYun.Configuration
{
    public class ConfigurationProvider
    {
        private readonly IConfigurationBuilder _configurationBuilder;

        public ConfigurationProvider()
        {
            _configurationBuilder = new ConfigurationBuilder().AddDefaultJsonFile();
        }

        public ConfigurationProvider SetBasePath(string basePath)
        {
            _configurationBuilder.SetBasePath(basePath);

            return this;
        }

        /// <summary>
        /// 通过命令行参数获取配置文件，如果命令行参数找不到，再通过环境变量获取配置文件
        /// </summary>
        public ConfigurationProvider AddEnvironmentJsonFile(IEnumerable<string> args, string environmentName = HostingEnvironment.DefaultEnvironmentName)
        {
            _configurationBuilder.AddEnvironmentJsonFile(args, environmentName);

            return this;
        }

        /// <summary>
        /// 通过环境变量获取配置文件
        /// </summary>
        public ConfigurationProvider AddEnvironmentJsonFile(string environmentName = HostingEnvironment.DefaultEnvironmentName)
        {
            _configurationBuilder.AddEnvironmentJsonFile(environmentName);

            return this;
        }

        /// <summary>
        /// 获取嵌入的配置文件
        /// </summary>
        public ConfigurationProvider AddEmbeddedJsonFile(string path)
        {
            _configurationBuilder.AddEmbeddedJsonFile(path);

            return this;
        }

        /// <summary>
        /// 获取带路径的配置文件
        /// </summary>
        public ConfigurationProvider AddPhysicalJsonFile(string path, string folder = null)
        {
            _configurationBuilder.AddPhysicalJsonFile(path, folder);

            return this;
        }

        public ConfigurationProvider AddCommandLine(string[] args)
        {
            _configurationBuilder.AddCommandLine(args);

            return this;
        }

        public ConfigurationProvider AddEnvironmentVariables(string prefix = null)
        {
            if (prefix == null)
            {
                _configurationBuilder.AddEnvironmentVariables();
            }
            else
            {
                _configurationBuilder.AddEnvironmentVariables(prefix);
            }

            return this;
        }

        public IConfiguration Build()
        {
            return _configurationBuilder.Build();
        }
    }
}
